package com.site.util;

import java.util.ArrayList;

/**
 * 主键生成策略
 * @author Believe fate
 *
 */
public class Sequences {
   /*
    * 添加一个主键类型
    */
   public static boolean addKey(String name,String defaultValue){
	   String sql = "INSERT INTO sequences VALUES (?,?)";
	   ArrayList<Object> pom = new ArrayList<Object>();
	   pom.add(name);
	   pom.add(defaultValue);
	   int count = 0;
	   //int count = Factory.mysqlDB.ExUpdateSQL(sql, pom);
	   if(count > 0){
		   return true;
	   }else{
		   return false;
	   }
   }
   /*
	* 更新主键键值  
	*/
   public static boolean updateKey(String name,String value){
	   String sql = "UPDATE sequences SET SEValue = ? WHERE SEName = ?";
	   ArrayList<Object> pom = new ArrayList<Object>();
	   pom.add(value);
	   pom.add(name);
	   int count = 0;
	   //int count = Factory.mysqlDB.ExUpdateSQL(sql, pom);
	   if(count > 0){
	 	   return true;
	   } else {
		   return false;
	   }
    }
   /*
    * 获取一个主键值 
    * 获取后更新到数据库 
    */
   public static String getKeyValue(String name){
	   String oldKey = "";
	   String newKey = "";
	   String sql = "SELECT SEValue FROM sequences WHERE SEName = ?";
	   ArrayList<Object> pom = new ArrayList<Object>();
	   pom.add(name);
//	   ResultSet result = Factory.mysqlDB.ExSelectSQL(sql, pom);
//	   try {
//		if(result.next()){
//			 oldKey = result.getString("SEValue");  
//		   }
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
		String [] str = oldKey.split("-");
        str[1] = Integer.toString((Integer.parseInt(str[1])+1));
        newKey = str[0]+"-"+str[1];
        // 更新主键值 
        Sequences.updateKey(name,newKey);       
	    return newKey;
   }
   /*
    * 删除一个主键类型，暂不提供 
    */ 
   public static boolean delKey(String name){
	   return false;
   }
}
